<?php
require_once 'controllers/controller.php';
require_once 'models/entite_historique_manager.php';
require_once 'models/zone_geographique_manager.php';
require_once 'models/date_historique.php';

class controller_entite_historique extends Controller
{
	protected $_entite;
	protected $_zoneGeo;
	
	public function __construct() {
		parent::__construct();
		$this->_entite = new entite_historique_manager($this->_db);
		$this->_zoneGeo = new zone_geographique_manager($this->_db);
	}
	
	public function hello() {
		$_SESSION['message'] = 'hello world !';
	}
	
	public function show() {
		if(!isset($_GET['id'])) {
			header('Location: ?section=entite_historique&action=show_all');
			exit();
		}
		$entite = $this->_entite->get_by_id($_GET['id']);
		$creations = $this->_entite->get_all_date_creation($_GET['id']);
		$dissolutions = $this->_entite->get_all_date_dissolution($_GET['id']);
		if(isset($_GET['date'])) {
			$population = $this->_entite->get_population($_GET['id'], $_GET['date']);
			if($population=="")$population="Inconnue pour cette date";
			$data = array();
			
			$zones_occ = $this->_zoneGeo->get_all_occ($_GET['id']);
			foreach($zones_occ as $z){
				if($z->exist_in($_GET['date'])) {
					$data[] = array("ISO" => $z->get_codeiso(),
							"ID" => $entite->get_identite(),
							"TEXT" => $z->get_nomzonegeo());
				}
			}
			
		}
		else {
			$zones = $this->_zoneGeo->get_all();
			$zones_occ = $this->_zoneGeo->get_all_occ($_GET['id']);
			$population = $this->_entite->get_average_population($_GET['id']);
		}
		include 'views/entite_historique_show.php';
	}
	
	public function show_all() {
		$entites = $this->_entite->get_all();
		include 'views/entite_historique_show_all.php';
		echo "<br><br>";
		if(isset($_SESSION['isAdmin']))
			include 'views/entite_historique_create.php';
	}
	
	public function add() {
		if(!isset($_POST['type']) || !isset($_POST['nom']) || !isset($_POST['profondeur'])) {
			header('Location: ?section=entite_historique&action=show_all');
			exit();
		}
		if(!date_historique::preg_date($_POST['creation']))
			$_POST['creation'] = "";
		if(!date_historique::preg_date($_POST['dissolution']))
			$_POST['dissolution'] = "";;

		$id = $this->_entite->max_id() + 1;
		$e = new entite_historique(array(
				"idEntite" => $id, 
				"typeEntite" => htmlspecialchars($_POST['type']),
				"nomEntite" => str_replace("'", "''", htmlspecialchars($_POST['nom'])),
				"descriptifEntite" => str_replace("'", "''", htmlspecialchars($_POST['description'])), 
				"wikiEntite" => htmlspecialchars($_POST['wiki']), 
				"profondeur" => $_POST['profondeur'], 
				"creation" => $_POST['creation'], 
				"dissolution" => $_POST['dissolution']));
		$this->_entite->add($e);

		header('Location: ?section=entite_historique&action=show_all');
		exit();
	}
	
	public function delete() {
		if(!isset($_POST['id'])) {
			header('Location: ?section=entite_historique&action=show_all');
			exit();
		}
		$this->_entite->delete($_POST['id']);
		
		header('Location: ?section=entite_historique&action=show_all');
		exit();
	}
	
	public function update() {
		if(!isset($_POST['id'])) {
			header('Location: ?section=entite_historique&action=show_all');
			exit();
		}
		if(!isset($_POST['type']) || !isset($_POST['nom']) || !isset($_POST['profondeur'])) {
			header('Location: ?section=entite_historique&action=show&id=' . $_POST['id']);
			exit();
		}
		if(!isset($_POST['creation']))
			$_POST['creation'] = "";		
		if(!isset($_POST['dissolution']))
			$_POST['dissolution'] = "";

		$e = new entite_historique(array(
				"idEntite" => $_POST['id'], 
				"typeEntite" => htmlspecialchars($_POST['type']),
				"nomEntite" => str_replace("'", "''",htmlspecialchars($_POST['nom'])),
				"descriptifEntite" => str_replace("'", "''", htmlspecialchars($_POST['description'])), 
				"wikiEntite" => htmlspecialchars($_POST['wiki']), 
				"profondeur" => $_POST['profondeur'], 
				"creation" => $_POST['creation'], 
				"dissolution" => $_POST['dissolution']));
		$this->_entite->update($e);
		
		header('Location: ?section=entite_historique&action=show&id=' . $_POST['id']);
		exit();
	}
	
	public function add_creation() {
		if(!isset($_POST['id'])){
			header('Location: ?section=entite_historique&action=show_all');
			exit();		
		}
		if(!isset($_POST['creation']) || !date_historique::preg_date($_POST['creation'])) {
			header('Location: ?section=entite_historique&action=show&id=' . $_POST['id']);
			exit();
		}
		
		$this->_entite->add_creation($_POST['id'], $_POST['creation']);
				header('Location: ?section=entite_historique&action=show&id=' . $_POST['id']);
		exit();
	}
	
	public function add_dissolution() {
		if(!isset($_POST['id'])){
			header('Location: ?section=entite_historique&action=show_all');
			exit();		
		}
		if(!isset($_POST['dissolution']) || !date_historique::preg_date($_POST['dissolution'])) {
			header('Location: ?section=entite_historique&action=show&id=' . $_POST['id']);
			exit();
		}
		
		$this->_entite->add_dissolution($_POST['id'], $_POST['dissolution']);
		header('Location: ?section=entite_historique&action=show&id=' . $_POST['id']);
		exit();
	}
	
	public function delete_date() {
		if(!isset($_POST['id'])){
			header('Location: ?section=entite_historique&action=show_all');
			exit();		
		}
		if(!isset($_POST['iddate'])) {
			header('Location: ?section=entite_historique&action=show&id=' . $_POST['id']);
			exit();
		}
		
		$this->_entite->delete_date($_POST['iddate']);
		header('Location: ?section=entite_historique&action=show&id=' . $_POST['id']);
		exit();
	}

	public function add_zone() {
		if(!isset($_POST['id'])) {
			header('Location: ?section=entite_historique&action=show_all');
			exit();			
		}
		if(!isset($_POST['zone'])) {
			header('Location: ?section=entite_historique&action=show&id=' . $_POST['id']);
			exit();		
		}
		
		if(!isset($_POST['debut']) || !date_historique::preg_date($_POST['debut'])) {
			$_POST['debut'] = "";
		}
		if(!isset($_POST['fin']) || !date_historique::preg_date($_POST['fin'])) {
			$_POST['fin'] = "";
		}
		$this->_entite->add_zone($_POST['id'], $_POST['zone'], $_POST['debut'], $_POST['fin']);
		
		header('Location: ?section=entite_historique&action=show&id=' . $_POST['id']);
		exit();
	}
	
	public function delete_zone() {
		if(!isset($_POST['id'])) {
			header('Location: ?section=entite_historique&action=show_all');
			exit();			
		}
		if(!isset($_POST['zone'])) {
			header('Location: ?section=entite_historique&action=show&id=' . $_POST['id']);
			exit();		
		}
		$this->_entite->delete_zone($_POST['zone']);
		
		header('Location: ?section=entite_historique&action=show&id=' . $_POST['id']);
		exit();
	}
	
	public function compare_map(){
		include 'views/date_show.php';
		if(isset($_GET['date'])){
			$data = array();
			foreach($this->_entite->get_all() as $entite){
				$zones_occ = $this->_zoneGeo->get_all_occ($entite->get_identite());
				foreach($zones_occ as $z){
					if($z->exist_in($_GET['date'])) {
						$data[] = array("ISO" => $z->get_codeiso(),
							"ID" => $entite->get_identite(),
							"TEXT" => $entite->get_nomentite());
					}
				}
			}
			include 'views/afficher_carte.php';
		}
	}
}
?>
